package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.ListData;
import com.cskaoyan.bean.OptionsVo;
import com.cskaoyan.bean.Role;
import com.cskaoyan.bean.bo.PermissionsBo;
import com.cskaoyan.bean.vo.PermissionsVo;
import com.cskaoyan.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author
 * @date 2020/12/6 19:34
 */
@RestController
@RequestMapping("admin/role")
public class RoleController {
    @Autowired
    RoleService roleService;

    @GetMapping("list")
    public BaseRespVo feedbackList(Integer page, Integer limit, String sort, String order, String name) {
        ListData<Role> listData =  roleService.queryRoleList(page, limit, sort, order,name);
        return BaseRespVo.ok(listData);
    }

    @GetMapping("options")
    public BaseRespVo options(){
        List<OptionsVo> data = roleService.selectValueAndLable();
        return BaseRespVo.ok(data);
    }

    @PostMapping("create")
    public BaseRespVo create(@RequestBody Role role){
        Role data = roleService.insertRole(role);
        return BaseRespVo.ok(data);
    }


    @PostMapping("update")
    public BaseRespVo update(@RequestBody Role role){
        roleService.updateRole(role);
        return BaseRespVo.ok();
    }

    //TODO
    @GetMapping("permissions")
    public BaseRespVo permissions(String roleId){
        PermissionsVo data = roleService.selectPermissions(roleId);
        return BaseRespVo.ok(data);
    }
    @PostMapping("permissions")
    public BaseRespVo permissions(@RequestBody PermissionsBo permissionsBo){
        roleService.deletePermission(permissionsBo);
        roleService.insertPermission(permissionsBo);
        return BaseRespVo.ok();
    }

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Role role){
        Integer id = role.getId();
        int num = roleService.countManager(id);
        if (num > 0) {
            return BaseRespVo.fail642();
        }
        roleService.deleteRole(role);
        return BaseRespVo.ok();
    }


}
